function gs_saveAsc(GSS,CommonPart,optVec)
%gs_saveAsc - saving GridStrain results as ASCII .asc ArcGIS-style files
%
%           gs_saveAsc(GSS,CommonPart,optVec)
%
% This function allows the saving of data related to a GSS struct variable. 
% Each generated file is an .asc ASCII file with ArcGIS-stile, with a six
% rows header and a data matrix. The header is:
%    ncols           xxx
%    nrows           xxx
%    xllcorner       xxx
%    yllcorner       xxx
%    cellsize        xxx
%    NODATA_value    xxx
%
% where
%   
%   - ncol and nrows are the integer number of grid lines along X-axis and
%     Y-axis respectively (taken from the matrices stored in GSS)
%   - xllcorner and yllcorner) are the X-coordinate and Y-coordinate of the 
%     grid origin, taken from GSS.DownLeftCorner
%   - cellsize is the cell size, taken from GSS.DownLeftCorner and 
%     GSS.TopRightCorner (note that a square grid is required)
%   - NODATA_value is the value assigned to NaN matrix elements (-9999).
%
% If the string CommonPart is assigned, the data saving procedure is 
% completely automatic. In this case, each file has name
%       (CommonPart)(TypeofData)(ScaleFactor).asc
% For example, if CommonPart is 'MyArea', TypeofData is 'MaxStrain' and
% the scale factor is 40000 m, the generated file is
%       MyAreaMaxStrain40000.asc.
% If CommonPart is undefined or empty, each data saving is interactively
% managed.
% The input optVec is a vector with integer numbers in the range 1:14.
% If optVec = 1:14, all files are generated. If OptVec = [n1 n2 ... nr],
% with n1, n2, nr natural numbers in the range 1:14, the file related
% to these indices are generated (see below the list).
% If optVec is undefined or empty, a list dalog box allow the choice of
% the files to be generated (multiple choice is allowed. All files can 
% also be generated).
% List:
%         1.  Maximum strain (rate) field
%         2.  Error on maximum strain (rate) field
%         3.  Minimum strain (rate) field
%         4.  Error on minimum strain (rate) field
%         5.  Azimuth strain (rate) field
%         6.  Number matrix
%         7.  Computation flag
%         8.  Significance flag
%         9.  U translation (velocity) field
%         10. Error on U translation (velocity) field
%         11. V translation (velocity) field
%         12. Error on V translation (velocity) field
%         13. Rotation field
%         14. Error on rotation field

% G. Teza, 2022.

nodataval = -9999;

if nargin < 3
    optVec = [];
end

if isempty(optVec)
    [optVec,tVec] = listdlg('ListString',...
        {'1. Maximum strain (rate) field',...
        '2. Error on maximum strain (rate) field',...
        '3. Minimum strain (rate) field',...
        '4. Error on minimum strain (rate) field',...
        '5. Azimuth strain (rate) field',...
        '6. Number matrix',...
        '7. Computation flag',...
        '8. Significance flag',...
        '9. U translation (velocity) field',...
        '10. Error on U translation (velocity) field',...
        '11. V translation (velocity) field',...
        '12. Error on V translation (velocity) field',...
        '13. Rotation field',...
        '14. Error on rotation field'},...
        'ListSize',[220 280]);
    if ~tVec
        optVec = [];
    end
end

if isempty(optVec)
    return
end

d0 = GSS.ScaleFactor;
sd0 = sprintf('%d',round(d0));

fv = GSS.DownLeftCorner;
xmi = fv(1);
ymi = fv(2);

lv = GSS.TopRightCorner;
xma = lv(1);
yma = lv(2);

if ismember(1,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'MaxStrain' sd0 '.asc'];
    else
        filena = savecond('','.asc','Maximum strain file');
    end
    if ~isempty(filena)
        EMAX = GSS.MaxStrainMatrix;
        ascFileWrite(EMAX,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(2,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'MaxStrainError' sd0 '.asc'];
    else
        filena = savecond('','.asc','Error maximum strain file');
    end
    if ~isempty(filena)
        eEMAX = GSS.MaxStrainMatrixError;
        ascFileWrite(eEMAX,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(3,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'MinStrain' sd0 '.asc'];
    else
        filena = savecond('','.asc','Minimum strain file');
    end
    if ~isempty(filena)
        EMIN = GSS.MinStrainMatrix;
        ascFileWrite(EMIN,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(4,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'MinStrainError' sd0 '.asc'];
    else
        filena = savecond('','.asc','Error minimum strain file');
    end
    if ~isempty(filena)
        eEMIN = GSS.MinStrainMatrixError;
        ascFileWrite(eEMIN,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(5,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'Azimuth' sd0 '.asc'];
    else
        filena = savecond('','.asc','Azimuth file');
    end
    if ~isempty(filena)
        PHI = GSS.AzimuthStrainMatrix;
        ascFileWrite(PHI,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(6,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'Number' sd0 '.asc'];
    else
        filena = savecond('','.asc','Number matrix file');
    end
    if ~isempty(filena)
        numb = GSS.NumberMatrix;
        ascFileWrite(numb,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(7,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'CompFlag' sd0 '.asc'];
    else
        filena = savecond('','.asc','Computation flag file');
    end
    if ~isempty(filena)
        FLC = GSS.ComputationFlag;
        ascFileWrite(FLC,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(8,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'SignFlag' sd0 '.asc'];
    else
        filena = savecond('','.asc','Significance flag file');
    end
    if ~isempty(filena)
        FLS = GSS.SignificanceFlag;
        ascFileWrite(FLS,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(9,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'UTranslation' sd0 '.asc'];
    else
        filena = savecond('','.asc','UTranslation file');
    end
    if ~isempty(filena)
        U0 = GSS.UTranslation;
        ascFileWrite(U0,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(10,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'UTranslationError' sd0 '.asc'];
    else
        filena = savecond('','.asc','Error UTranslation file');
    end
    if ~isempty(filena)
        eU0 = GSS.UTranslationError;
        ascFileWrite(eU0,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(11,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'VTranslation' sd0 '.asc'];
    else
        filena = savecond('','.asc','VTranslation file');
    end
    if ~isempty(filena)
        V0 = GSS.VTranslation;
        ascFileWrite(V0,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(12,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'VTranslationError' sd0 '.asc'];
    else
        filena = savecond('','.asc','Error VTranslation file');
    end
    if ~isempty(filena)
        eV0 = GSS.VTranslationError;
        ascFileWrite(eV0,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(13,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'Rotation' sd0 '.asc'];
    else
        filena = savecond('','.asc','Rotation file');
    end
    if ~isempty(filena)
        OMEG = GSS.Rotation;
        ascFileWrite(OMEG,xmi,xma,ymi,yma,filenout,nodataval);
    end
end
if ismember(14,optVec)
    if ~isempty(CommonPart)
        filena = [CommonPart 'RotationError' sd0 '.asc'];
    else
        filena = savecond('','.asc','Rotation file');
    end
    if ~isempty(filena)
        eOMEG = GSS.RotationError;
        ascFileWrite(eOMEG,xmi,xma,ymi,yma,filenout,nodataval);
    end
end